/*
 * $Id$
 */
package py.firefly.resolution;

import java.util.HashMap;
import java.util.Map;
import py.firefly.FireflyException;

import py.firefly.request.Request;
import py.firefly.request.Response;

/**
 *
 * @author myang
 */
public class Tiles implements Resolution {

    public static final String TILES = "__ff_tiles__";

    public static final String TILES_TEMPLATE = "__ff_tiles_template__";

    private final String _page;

    private final String _contentType = "text/html;charset=utf-8";

    private final Map<String, Object> _tiles = new HashMap();

    /**
     * 
     */
    public Tiles() {
        this(null);
    }

    /**
     * 
     * @param page 
     */
    public Tiles(final String page) {
//        this(page, "text/html;charset=utf-8");
        _page = page;
//        _contentType = contentType;
    }

//    public Tiles(final String page, final String contentType) {
//        _page = page;
//        _contentType = contentType;
//    }

    /**
     * 
     * @param req
     * @param resp 
     */
    @Override
    public void execute(final Request req, final Response resp) {
        final String template = template(req);
        req.attr(TILES, _tiles)
          .forward(template, resp.contentType(_contentType));
    }

    /**
     * 
     * @param req
     * @return 
     */
    private String template(final Request req) {
        final String s = 
          (_page == null ? (String)req.context(TILES_TEMPLATE) : _page);
        if (null == s) {
            throw new FireflyException("no template");
        }
        return s;
    }

    /**
     * 
     * @param name
     * @param val
     * @return 
     */
    public Tiles put(final String name, final Object val) {
        _tiles.put(name, val);
        return this;
    }
    
}
